/**
 * Copyright 2009. Felix Dorner.
 * This code is licensed under the Eclipse Public License v1.0
 * http://www.eclipse.org/org/documents/epl-v10.html
 *
 * $Id$
 */
package com.objectcarpentry.kaylee.impl;

import java.util.regex.Pattern;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.impl.ENotificationImpl;

import com.objectcarpentry.kaylee.IPlayableFilter;
import com.objectcarpentry.kaylee.IPlayer;
import com.objectcarpentry.kaylee.IPlayerEvent;
import com.objectcarpentry.kaylee.IPlayerListener;
import com.objectcarpentry.kaylee.KayleePackage;
import com.objectcarpentry.kaylee.PlayableState;
import com.objectcarpentry.kaylee.PlayerEventType;
import com.objectcarpentry.kaylee.Track;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Track</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.objectcarpentry.kaylee.impl.TrackImpl#getTitle <em>Title</em>}</li>
 *   <li>{@link com.objectcarpentry.kaylee.impl.TrackImpl#getArtist <em>Artist</em>}</li>
 *   <li>{@link com.objectcarpentry.kaylee.impl.TrackImpl#getLocation <em>Location</em>}</li>
 *   <li>{@link com.objectcarpentry.kaylee.impl.TrackImpl#getHash <em>Hash</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class TrackImpl extends PlayableImpl implements Track {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public static final String copyright = "Copyright 2009. Felix Dorner.\nThis code is licensed under the Eclipse Public License v1.0\nhttp://www.eclipse.org/org/documents/epl-v10.html";

	/**
	 * The default value of the '{@link #getTitle() <em>Title</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTitle()
	 * @generated
	 * @ordered
	 */
	protected static final String TITLE_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getTitle() <em>Title</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTitle()
	 * @generated
	 * @ordered
	 */
	protected String title = TITLE_EDEFAULT;

	/**
	 * The default value of the '{@link #getArtist() <em>Artist</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getArtist()
	 * @generated
	 * @ordered
	 */
	protected static final String ARTIST_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getArtist() <em>Artist</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getArtist()
	 * @generated
	 * @ordered
	 */
	protected String artist = ARTIST_EDEFAULT;

	/**
	 * The default value of the '{@link #getLocation() <em>Location</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLocation()
	 * @generated
	 * @ordered
	 */
	protected static final URI LOCATION_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getLocation() <em>Location</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLocation()
	 * @generated
	 * @ordered
	 */
	protected URI location = LOCATION_EDEFAULT;

	/**
	 * The default value of the '{@link #getHash() <em>Hash</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getHash()
	 * @generated
	 * @ordered
	 */
	protected static final byte[] HASH_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getHash() <em>Hash</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getHash()
	 * @generated
	 * @ordered
	 */
	protected byte[] hash = HASH_EDEFAULT;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected TrackImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return KayleePackage.Literals.TRACK;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setTitle(String newTitle) {
		String oldTitle = title;
		title = newTitle;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, KayleePackage.TRACK__TITLE, oldTitle, title));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getArtist() {
		return artist;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setArtist(String newArtist) {
		String oldArtist = artist;
		artist = newArtist;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, KayleePackage.TRACK__ARTIST, oldArtist, artist));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public URI getLocation() {
		return location;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setLocation(URI newLocation) {
		URI oldLocation = location;
		location = newLocation;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, KayleePackage.TRACK__LOCATION, oldLocation, location));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public byte[] getHash() {
		return hash;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setHash(byte[] newHash) {
		byte[] oldHash = hash;
		hash = newHash;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, KayleePackage.TRACK__HASH, oldHash, hash));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case KayleePackage.TRACK__TITLE:
				return getTitle();
			case KayleePackage.TRACK__ARTIST:
				return getArtist();
			case KayleePackage.TRACK__LOCATION:
				return getLocation();
			case KayleePackage.TRACK__HASH:
				return getHash();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case KayleePackage.TRACK__TITLE:
				setTitle((String)newValue);
				return;
			case KayleePackage.TRACK__ARTIST:
				setArtist((String)newValue);
				return;
			case KayleePackage.TRACK__LOCATION:
				setLocation((URI)newValue);
				return;
			case KayleePackage.TRACK__HASH:
				setHash((byte[])newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case KayleePackage.TRACK__TITLE:
				setTitle(TITLE_EDEFAULT);
				return;
			case KayleePackage.TRACK__ARTIST:
				setArtist(ARTIST_EDEFAULT);
				return;
			case KayleePackage.TRACK__LOCATION:
				setLocation(LOCATION_EDEFAULT);
				return;
			case KayleePackage.TRACK__HASH:
				setHash(HASH_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case KayleePackage.TRACK__TITLE:
				return TITLE_EDEFAULT == null ? title != null : !TITLE_EDEFAULT.equals(title);
			case KayleePackage.TRACK__ARTIST:
				return ARTIST_EDEFAULT == null ? artist != null : !ARTIST_EDEFAULT.equals(artist);
			case KayleePackage.TRACK__LOCATION:
				return LOCATION_EDEFAULT == null ? location != null : !LOCATION_EDEFAULT.equals(location);
			case KayleePackage.TRACK__HASH:
				return HASH_EDEFAULT == null ? hash != null : !HASH_EDEFAULT.equals(hash);
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (title: ");
		result.append(title);
		result.append(", artist: ");
		result.append(artist);
		result.append(", location: ");
		result.append(location);
		result.append(", hash: ");
		result.append(hash);
		result.append(')');
		return result.toString();
	}

	@Override
	public void playOn(final IPlayer player, Object parameters) {
		player.setInput(java.net.URI.create(getLocation().toString()));
		player.addPlayerListener(new IPlayerListener(){
			@Override
			public void handleEvent(IPlayerEvent e) {
				switch (e.getType()) {
				
				case PAUSE: {
					setState(PlayableState.PAUSED);
					break;
				}
				case PLAY: {
					setState(PlayableState.PLAYING);
					break;
				}
				case STOP: 
				case INPUT:
				case EOS: 
				case ERROR: {
					player.removePlayerListener(this);
					setState(PlayableState.STOPPED);
					break;
				}
				}
			} 
		});
		player.play();
	}

	
	@Override
	public boolean match(IPlayableFilter filter) {
		if (filter == null){
			return true;
		}
		
		return ( find(getArtist(), filter.getArtist()) || find(getTitle(), filter.getTitle()));
		
	}
	
	private boolean find(String test, Pattern pattern){
		
		boolean found = false;
		if (pattern == null){
			found = true;
		} else if (test != null) {
			found = pattern.matcher(test).find();
		}
		
		
		return found;
		
	}

} //TrackImpl
